package com.es.system;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;

import com.es.main.HrmMenu;
import com.es.util.db.Dao;

public class SysBiz {

	@SuppressWarnings("unchecked")
	public ArrayList<HashMap<String,String>> getRoleList()throws Exception {

		ArrayList<HashMap<String,String>> roleList = new ArrayList<HashMap<String,String>>();;
		Dao dao = new Dao();
		String sql = "select common_code,common_value from  hrm_common where common_type='role' and common_code!='2' ";
		try {
			ResultSet resultSet = dao.Select(sql);
			while(resultSet.next()){
				HashMap<String,String> map = new HashMap<String,String>();
				map.put("commonCode", resultSet.getString("common_code"));
				map.put("commonValue", resultSet.getString("common_value"));
				roleList.add(map);
			}
		} finally{
			dao.close();
		}
		return roleList;
	}
	
	@SuppressWarnings("unchecked")
	public ArrayList<HrmMenu> getMenuAllList()throws Exception {

		Dao dao = new Dao();
		HrmMenu menu = null;
	    ArrayList<HrmMenu> menuList = new ArrayList<HrmMenu>();
	    ArrayList<String> queryParm = new ArrayList<String>();
	    
	    String sql = "select m.menu_id,m.menu_name,m.parent_id,m.menu_url,m.menu_desc  from hrm_menu m order by m.menu_id";
	    try {
	    	ResultSet rs = dao.Select(sql,queryParm);
	      while (rs.next())
	      {
	        menu = new HrmMenu();
	        menu.setMenuId(rs.getString("menu_id"));
	        menu.setMenuName(rs.getString("menu_name"));
	        menu.setParentId(rs.getString("parent_id"));
	        menu.setMenuUrl(rs.getString("menu_url"));
	        menu.setMenuDesc(rs.getString("menu_desc"));
	        menuList.add(menu);
	      }
	    }
	    finally {
	    	dao.close();
	    }
	   /* if(menuList.size()==0)
	    	return null;*/
	    return menuList;
	}
	
	@SuppressWarnings("unchecked")
	public boolean manageMenu(String roleId,String result)throws Exception {

		Dao dao = new Dao();
	    ArrayList<String> delParms = new ArrayList<String>();
	    ArrayList<String> addParms = new ArrayList<String>();
	    
	    String delSql = "delete from hrm_menu_role where role_id=?";
	    String addSql = "insert into hrm_menu_role(role_id,menu_id) values(?,?)";
	    delParms.add(roleId);
	   try{
		   dao.update(delSql,delParms);
		   if(result!=null&&!result.equals("")){
			   String[] parms = result.split(","); 		   
			   for(int i=0;i<parms.length;i++){
				   addParms.add(roleId);
				   addParms.add(parms[i]);
				   dao.update(addSql,addParms);
				   addParms.clear();
			   }	
		   }
	   }catch(Exception e){
		   return false;
	   }finally {		   
		   dao.close();
	    }
	    return true;
	}
	
	public boolean deleteEmpLogin(String emp_id)throws Exception {

		if(emp_id==null) return false;
		
		Dao dao = new Dao();
	    ArrayList<String> query = new ArrayList<String>();
	    query.add(emp_id);
	    String delSql = "update hrm_emp_info set emp_login_yn='N' where emp_id=? ";
	   try{
		   dao.update(delSql,query);
	   }catch(Exception e){
		   return false;
	   }finally {		   
		   dao.close();
	    }
	    return true;
	}	

	public boolean registerEmpPwd(String emp_id,String Pwd)throws Exception {

		if(emp_id==null) return false;
		
		Dao dao = new Dao();
	    ArrayList<String> query = new ArrayList<String>();
	    query.add(Pwd);
	    query.add(emp_id);	   
	    String delSql = "update hrm_emp_info set emp_login_yn='Y',emp_login_pwd = ? where emp_id=? ";
	   try{
		   dao.update(delSql,query);
	   }catch(Exception e){
		   return false;
	   }finally {		   
		   dao.close();
	    }
	    return true;
	}
}
